package top.yangbuyi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class QueryAddressHelper {

	  public static void main(String[] args) {
			// write your code here
			String phone = "18570605417";
			String city = QueryAddressByPhone(phone);
			System.out.println(city);

			//等待的时间较长
			String IP = "36.158.146.79";
			String city2 = QueryAddressByIP(IP);
			System.out.println(city2);
	  }

	  public static String QueryAddressByIP(String IP) {
			//https://www.cnblogs.com/nul1/p/11525943.html
			String style_simcall = "归属地未知";
			String httpUrl = "http://ip-api.com/json/";
			String jsonResult = request(httpUrl, IP + "?lang=zh-CN");
			//System.out.println(jsonResult);
			JSONObject json = JSON.parseObject(jsonResult);
			System.out.println(json);
			String status = json.getString("status");
			if ("success".equals(status)) {
				  String regionName = json.getString("regionName");
				  String city = json.getString("city");
				  style_simcall = String.format("%s,%s", regionName, city);
			}
			return style_simcall;
	  }

	  //------------------------------------------------------------

	  public static String QueryAddressByPhone(String phone) {
			String style_simcall = "归属地未知";
			String httpUrl = "http://api.k780.com:88/?app=phone.get&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
			String jsonResult = request(httpUrl, "&phone=" + phone);
			//{"success":"1",
			// "result":
			// {
			// "status":"ALREADY_ATT",
			// "phone":"18601409800",
			// "area":"025",
			// "postno":"210000",
			// "att":"中国,江苏,南京",
			// "ctype":"中国联通186卡",
			// "par":"1860140","prefix":"186",
			// "operators":"中国联通",
			// "style_simcall":"中国,江苏,南京",
			// "style_citynm":"中华人民共和国,江苏省,南京市"
			// }
			// }
			//  System.out.println(jsonResult);
			//因为json字符串是大括号包围，所以用JSONObject解析
			JSONObject json = JSON.parseObject(jsonResult);
			String success = json.getString("success");
			if ("1".equals(success)) {
				  String result = json.getString("result");
				  AddressInfo address = JSON.parseObject(result, AddressInfo.class);
				  if (address != null) {
						style_simcall = address.getStyle_simcall();
				  }
			}

			return style_simcall;
	  }

	  /**
	   * @param urlAll  :请求接口
	   * @param httpArg :参数
	   * @return 返回结果
	   */
	  public static String request(String httpUrl, String httpArg) {
			BufferedReader reader = null;
			String result = null;
			StringBuffer sbf = new StringBuffer();
			httpUrl = httpUrl + httpArg;

			try {
				  URL url = new URL(httpUrl);
				  HttpURLConnection connection = (HttpURLConnection) url
						  .openConnection();
				  connection.setRequestMethod("GET");
				  // 填入apikey到HTTP header
				  //   connection.setRequestProperty("apikey", api_key);
				  connection.connect();
				  InputStream is = connection.getInputStream();
				  reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				  String strRead = null;
				  while ((strRead = reader.readLine()) != null) {
						sbf.append(strRead);
						sbf.append("\r\n");
				  }
				  reader.close();
				  result = sbf.toString();
			} catch (Exception e) {
				  e.printStackTrace();
				  return "链接超时.....";
			}
			return result;
	  }
}
